<template>
  <div class="notice-manage">
    <el-card>
      <div class="header-bar">
        <span>公告列表</span>
        <el-button type="primary" size="small" @click="openAddNotice">新增公告</el-button>
      </div>
      <el-table :data="noticeList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="title" label="公告标题" />
        <el-table-column prop="content" label="公告内容" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="openEditNotice(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDeleteNotice(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑公告弹窗 -->
    <el-dialog v-model="noticeDialogVisible" :title="noticeForm.id ? '编辑公告' : '新增公告'">
      <el-form :model="noticeForm" label-width="80px">
        <el-form-item label="公告标题">
          <el-input v-model="noticeForm.title" />
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input v-model="noticeForm.content" type="textarea" :rows="4" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="noticeForm.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="noticeDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveNotice">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getNoticeList, addNotice, updateNotice, deleteNotice } from '@/api/admin/notice'

const noticeList = ref([])
const noticeDialogVisible = ref(false)
const noticeForm = ref({ id: null, title: '', content: '', status: 1 })

const fetchNotices = async () => {
  const res = await getNoticeList()
  if (res.code === 200) {
    noticeList.value = res.data
  }
}

const openAddNotice = () => {
  noticeForm.value = { id: null, title: '', content: '', status: 1 }
  noticeDialogVisible.value = true
}

const openEditNotice = (row) => {
  noticeForm.value = { ...row }
  noticeDialogVisible.value = true
}

const handleSaveNotice = async () => {
  if (!noticeForm.value.title || !noticeForm.value.content) {
    ElMessage.warning('标题和内容不能为空')
    return
  }
  let res
  if (noticeForm.value.id) {
    res = await updateNotice(noticeForm.value)
  } else {
    res = await addNotice(noticeForm.value)
  }
  if (res.code === 200) {
    ElMessage.success('保存成功')
    noticeDialogVisible.value = false
    fetchNotices()
  } else {
    ElMessage.error(res.msg || '保存失败')
  }
}

const handleDeleteNotice = (row) => {
  ElMessageBox.confirm('确定要删除该公告吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await deleteNotice(row.id)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      fetchNotices()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  }).catch(() => {})
}

onMounted(() => {
  fetchNotices()
})
</script>

<style scoped>
.notice-manage {
  padding: 20px;
}
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
</style>
